package com.zsw.dubbo.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.ResourceBundle;


public class PropertyUtil {
	private ResourceBundle rb;

	public PropertyUtil(String dirName) {
		rb = ResourceBundle.getBundle(dirName);
	}

	public static Properties getProperties(Class<?> clazz, String dir) {
		Properties p = new Properties();
		try {
			p.load(clazz.getResourceAsStream(dir));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

	public static Properties getProperties(String dir) {
		Properties p = new Properties();
		try {
			p.load(ClassLoaderUtil.getResource(dir));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

	public String getUTF8(String key) {
		return get(key, "UTF-8");
	}

	public String get(String key) {
		return get(key, null);
	}

	public String get(String key, String charset) {
		String str = null;
		try {
			str = rb.getString(key);
		} catch (Exception e1) {
		}
		if (str != null) {
			if (charset != null) {
				try {
					return new String(str.getBytes("ISO-8859-1"), charset);
				} catch (UnsupportedEncodingException e) {
				}
			} else {
				return str;
			}
		}
		return key;
	}
}
